/* ========================================================================
 * PROJECT: FTIR Blob Tracker
 * ========================================================================
 * This work is based on the original FTIR Blob Tracker developed by
 *   Woonhyuk Baek
 *   Woontack Woo
 *   U-VR Lab, GIST of Gwangju in Korea.
 *   
 *   http://uvr.gist.ac.kr/
 *
 * Copyright of the derived and new portions of this work
 *     (C) 2009 GIST U-VR Lab.
 *
 * This framework is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This framework is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this framework; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * For further information please contact 
 *   Woonhyuk Baek
 *   <windage@live.com>
 *   GIST U-VR Lab.
 *   Department of Information and Communication
 *   Gwangju Institute of Science and Technology
 *   1, Oryong-dong, Buk-gu, Gwangju
 *   South Korea
 * ========================================================================
 ** @author   Woonhyuk Baek
 * ======================================================================== */

#ifndef BLOB_H
#define BLOB_H

#include <cv.h>

typedef struct _BlobPoint
{
	double x;
	double y;
	void operator=(struct _BlobPoint oprd)
	{
		this->x = oprd.x;
		this->y = oprd.y;
	}
	void operator/=(double oprd)
	{
		this->x /= oprd;
		this->y /= oprd;
	}
}BlobPoint;

class Blob
{
public:
	enum BLOB_EVENT_MESSAGE
	{
		MEMSSAGE_NONE = 0,
		MEMSSAGE_ADD,
		MEMSSAGE_MOVE,
		MEMSSAGE_HOLD,
		MEMSSAGE_REMOVE,
	};

public:
	int id;

	BlobPoint pt;
	BlobPoint lastPt;

	float area;
	float length;
	float angle;
	CvRect boundingRect;

	CvScalar color;
	double maccel;  //distance traveled since last frame
	double age;     //how long the blob has been at war
	double sitting; //how long hes been sitting in the same place
	double lastTimeTimeWasChecked;
	double downTime;

	BLOB_EVENT_MESSAGE eventMessage;
	
	
	Blob()
	{
		id = 0;

		pt.x = 0.0f;
		pt.y = 0.0f;
		lastPt.x = 0.0f;
		lastPt.y = 0.0f;

		area = 0.0f;
		length = 0.0f;
		angle = 0.0f;
		boundingRect = cvRect(0, 0, 0, 0);

		color = CV_RGB(255, 255, 255);
		maccel = 0.0f;
		age = 0.0f;
		sitting = 0.0f;
		lastTimeTimeWasChecked = 0;
		downTime = 0;

		eventMessage = Blob::MEMSSAGE_NONE;
	}

	void operator=(class Blob oprd)
	{
		this->id = oprd.id;

		this->pt = oprd.pt;
		this->lastPt = oprd.lastPt;

		this->area = oprd.area;
		this->length = oprd.length;
		this->angle = oprd.angle;
		this->boundingRect = oprd.boundingRect;

		this->color = oprd.color;
		this->maccel = oprd.maccel;
		this->age = oprd.age;
		this->sitting = oprd.sitting;
		this->lastTimeTimeWasChecked = oprd.lastTimeTimeWasChecked;
		this->downTime = oprd.downTime;
		
		this->eventMessage = oprd.eventMessage;
	}

	void Draw(IplImage* image)
	{
		cvDrawRect(image, cvPoint(boundingRect.x, boundingRect.y), cvPoint(boundingRect.x+boundingRect.width, boundingRect.y+boundingRect.height), color);
		cvLine(image, cvPoint((int)pt.x, (int)pt.y), cvPoint((int)lastPt.x, (int)lastPt.y), color, 2);
	}
};

#endif